<script setup>
import CommonSearch from "@/components/commonSearch/index.vue";
import CommonTable from "@/components/commonTable/index.vue";
import { useSearchTable } from "@/hooks/table/useSearchTable";
import { getWayfairLogList } from "@/api/configuration/wayfairLog.js";
import { message } from "ant-design-vue";
import { postFile } from "@utils/service/index.js";
import dayjs from "dayjs";

const { dataList, query, loading, page, search, reset, onPageChange } =
  useSearchTable({
    searchAPI: getWayfairLogList,
    queryFactory: () => ({}),
  });

const searchForm = ref([
  {
    label: "店铺sku",
    type: "input",
    field: "wfSku",
  },
  {
    label: "平台sku",
    type: "input",
    field: "productSku",
  },
  {
    label: "仓库编码",
    type: "input",
    field: "warehouseCode",
  },
]);
const tableHead = ref([
  {
    dataIndex: "wfSku",
    title: "店铺sku",
    align: "center",
    fieldType: "String",
  },
  {
    dataIndex: "productSku",
    title: "平台sku",
    align: "center",
    fieldType: "String",
  },
  {
    dataIndex: "warehouseCode",
    title: "仓库编码",
    align: "center",
    fieldType: "String",
  },
  {
    dataIndex: "warehouseId",
    title: "仓库id",
    align: "center",
    fieldType: "String",
  },
  {
    dataIndex: "qty",
    title: "库存数",
    align: "center",
    fieldType: "String",
  },
  {
    dataIndex: "updateWfTime",
    title: "最新更新wf时间",
    align: "center",
    fieldType: "String",
  },
]);

let exportLoading = ref(false);
const exportLogDetails = async () => {
  try {
    exportLoading.value = true;
    let obj = { ...query.value };
    await postFile(
      "/config/wayfair/export_log_details",
      "post",
      obj,
      `wayfair库存更新日志-${dayjs().format("YYYY-MM-DD")}.xlsx`
    );
    message.success("导出成功");
  } catch (e) {
    message.error(e);
  } finally {
    exportLoading.value = false;
  }
};
</script>

<template>
  <CommonSearch
    :show-item="3"
    :col-layout="{ xxl: 6, xl: 8, lg: 8, md: 12, sm: 24, xs: 24 }"
    v-model="query"
    :formItems="searchForm"
    @search="search"
    @reset="reset"
  ></CommonSearch>

  <a-button type="primary" :loading="exportLoading" @click="exportLogDetails">
    <template #icon>
      <DownloadOutlined />
    </template>
    导出
  </a-button>
  <CommonTable
    :customFilterDropdown="true"
    :columns="tableHead"
    :dataSource="dataList"
    v-model:localPagination="page"
    @paginationChange="onPageChange"
    v-model:loading="loading"
    :rowKey="(record) => record.wfSku+record.warehouseId"
    :scroll="{ x: 'max-content' }"
  ></CommonTable>
</template>
